package com.vegetable.common.async;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.vegetable.common.utils.Constant;
import com.vegetable.modules.entity.baseData.Products;
import com.vegetable.modules.entity.inbound.AgentDeliveryDetail;
import com.vegetable.modules.entity.inbound.PackDeliveryDetail;
import com.vegetable.modules.entity.inbound.PurchaseDeliveryDetail;
import com.vegetable.modules.mapper.baseData.ProductsMapper;
import com.vegetable.modules.service.inbound.IAgentDeliveryDetailService;
import com.vegetable.modules.service.inbound.IPackDeliveryDetailService;
import com.vegetable.modules.service.inbound.IPurchaseDeliveryDetailService;
import com.vegetable.modules.service.quotation.IQuotationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

import static com.vegetable.common.utils.Constant.StockFlowType.resolveFromPDType;

@Slf4j
@Service
public class AsyncStockService {
    @Resource
    private IQuotationService iQuotationService;
    @Resource
    private IAgentDeliveryDetailService iAgentDeliveryDetailService;
    @Resource
    private IPackDeliveryDetailService iPackDeliveryDetailService;
    @Resource
    private IPurchaseDeliveryDetailService iPurchaseDeliveryDetailService;
    @Resource
    private ProductsMapper productsMapper;

    @Async
    public void completeQuotation(Long commodityId, BigDecimal costWeight, Date orderTime, Long detailId, Integer orderType){
        log.info("开始处理入库商品对应的报价信息， {}, {}, {}, {}, {}", commodityId, costWeight, orderTime, detailId, orderType);
        Products products = productsMapper.selectById(commodityId);
        if(ObjectUtil.isNull(products)){
            log.info("根据{}找不到商品哦",commodityId);
            return;
        }
        if(ObjectUtil.isNull(products.getQuotationId())){
            log.info("{}({})根据{}找不到对应的报价商品哦", products.getCommonName(), commodityId);
            return;
        }
        Constant.StockFlowType flowType = resolveFromPDType(orderType);
        BigDecimal numWeight = BigDecimal.ZERO;
        switch (flowType){
            case BULK_IN:
                PurchaseDeliveryDetail pddB = iPurchaseDeliveryDetailService.getById(detailId);
                if(ObjectUtil.isNotNull(pddB)){
                    costWeight = pddB.getUnitPrice();
                }
                break;
            case WORK_IN:
                //
                PackDeliveryDetail pdd = iPackDeliveryDetailService.getById(detailId);
                if(ObjectUtil.isNotNull(pdd)){
                    Number num = pdd.getNumber() == 0 ? 1 : pdd.getNumber();
                    numWeight = NumberUtil.div(pdd.getMoney(), num);
                }
                break;
            case AGENT_IN:
                AgentDeliveryDetail add = iAgentDeliveryDetailService.getById(detailId);
                if(ObjectUtil.isNotNull(add)){
                    costWeight = add.getUnitPrice();
                }
                break;
            case LOGISTICS_IN:
                PurchaseDeliveryDetail pddL = iPurchaseDeliveryDetailService.getById(detailId);
                if(ObjectUtil.isNotNull(pddL)){
                    costWeight = pddL.getUnitPrice();
                }
                break;
            default:
                log.info("根据{}没有找到相关的入库类型。她的detailId是{}", orderType, detailId);
                break;
        }
//        Constant.StockFlowType stockFlowType = Constant.StockFlowType.getFromPDType();
        if(products.getQuotationId() == 0){
            log.info("入库商品{}没有设置报价信息", commodityId);
            return;
        }
        iQuotationService.updateQuotation(costWeight, products.getQuotationId(), orderTime, numWeight);
    }
}
